<template>
  <BasicTable
    title=""
    :columns="columns"
    :dataSource="data"
    :loading="loading"
    striped
    bordered
    showTableSetting
    :pagination="pagination"
    @change ="hangdleTableChange"
    :resizeHeightOffset="32"
  >
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'project'">
        <span class="">
          <a>{{projectMap[record.project.split("_")[1]].name}}</a>
        </span>
      </template>
      <template v-if="column.key === 'channel'">
        <span class="">
          <a>{{sensorMap[record.sensor].channel}}</a>
        </span>
      </template>
      <template v-if="column.key === 'ts'">
        <span class="">
          <a>{{record.ts.replace("T"," ") }}</a>
        </span>
      </template>
      <template v-if="column.key === 'sensor'">
        <span class="">
          <a>{{sensorMap[record.sensor].name}}</a>
        </span>
      </template>
    </template>
  </BasicTable>
</template>
  <script lang="ts">
import { defineComponent, ref } from 'vue';
import { BasicTable } from '/@/components/Table';
import { getCols } from './tableData';

export default defineComponent({
  components: { BasicTable },
  setup(props, { emit }) {
    const loading = ref(false);
    const pagination = ref<any>(true);
    const data = ref([]);
    const projectMap = ref({});
    const sensorMap = ref({});


    const setProjects = (projects:any)=>{
      let map = {};
      projects.forEach((item)=>{
        map[item.id+''] = item;
      })
      projectMap.value = map;
      console.log(projectMap.value[1])
    }

    const setSensorMap = (sensors:any)=>{
      let map = {};
      sensors.forEach((item)=>{
        map[item.id] = item;
      })
      sensorMap.value = map;
    }

    const setData = (data1:any,page:any) =>{
      data.value = data1;
      pagination.value = page;
    }

    const hangdleTableChange = (val)=>{
        emit('handleTableChange', val);
    }


    return {
      columns: getCols(),
      data,
      setProjects,
      projectMap,
      sensorMap,
      setSensorMap,
      setData,
      loading,
      pagination,
      hangdleTableChange
    };
  },
});
</script>
